<template>
  <view>
    <u-modal v-model="show" :content="content" @confirm="submitInfo">
      <uni-forms ref="form" :modelValue="formData" :rules="rules" validateTrigger="bind">
        <uni-forms-item label="姓名" required="true">
          <uni-easyinput v-model="nameValue" placeholder="请输入你的真实姓名" @change="getRealName" ></uni-easyinput>
        </uni-forms-item>
        <uni-forms-item label="账号" required="true">
          <uni-easyinput v-model="accountValue" placeholder="请输入你的电话号码" @change="getPhone"></uni-easyinput>
        </uni-forms-item>
        <uni-forms-item label="密码" required="true" name="secret">
          <uni-easyinput v-model="formData.secret" placeholder="请自定义你的密码" @change="getPassword" type="password"></uni-easyinput>
        </uni-forms-item>
      </uni-forms>
    </u-modal>
    
    <view class="top flex flex-direction justify-center align-center padding-top-lg" v-if="isLogin">
      <image class="cu-avatar xl round margin-bottom-sm" src='https://typora-licodeao.oss-cn-guangzhou.aliyuncs.com/typoraImg/Rococo2.jpg'></image>
      <view class="text-bold">{{isLogin ? '你已登录' : '未登录'}}</view>
      <view class="wave"></view>
    </view>
    <view class="top flex flex-direction justify-center align-center padding-top-lg" v-else>
      <image class="cu-avatar xl round margin-bottom-sm"></text></image>
      <button class="cu-btn block bg-green" @click="Login">微信登录</button>
      <view class="wave"></view>
    </view>
    <view class="cu-list menu sm-border card-menu margin-top">
      <view class="cu-item arrow" v-if="authority == '[ROLE_SADMIN]'">
        <view class="content" @click="changeAuthority">
          <text class="cuIcon-profile text-grey"></text>
          <text class="text-grey">更换身份</text>
        </view>
      </view>
      <view class="cu-item arrow">
        <view class="content" @click="toUpdate">
          <text class="cuIcon-newshot text-grey"></text>
          <text class="text-grey">更新日志</text>
        </view>
      </view>
      <view class="cu-item arrow">
        <view class="content" @click="toGuide">
          <text class="cuIcon-circle text-grey"></text>
          <text class="text-grey">使用指南</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        authority: uni.getStorageSync('authority'),
        serverUrl: this.$globalUrl,
        token: '',
        isLogin: uni.getStorageSync("isLogin"),
        show: false,
        userInfo: uni.getStorageSync("globalUser"),
        nameValue: '',
        accountValue: '',
        formData: {
          secret: ""
        },
        rules: {
          secret: {
            rules: [
              {
                maxLength: 8,
                errorMessage: '密码长度不超过 {maxLength} 位'
              }
            ]
          }
        },
        info: {
          avatarUrl: "",
          code: '',
          isFirst: false,
          nickName: "",
          phone: "",
          password: "",
          realName: uni.getStorageSync("realName")
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
      wx.checkSession({
        success() {
          console.log("未过期")
        },
        fail() {
          uni.removeStorageSync("isLogin")
          uni.removeStorageSync("globalUser")
          uni.removeStorageSync("token")
          uni.removeStorageSync("tokenHead")
          uni.removeStorageSync("realName")
        }
      })
    },
    methods: {
      toLoginPage() {
        wx.navigateTo({
          url: '../../subpkg/loginPage/loginPage'
        })
      },
      //微信登录
      Login() {
        let self = this
        uni.getUserProfile({
          desc: '登录',
          success: async (res) => {
            self.info.avatarUrl = 'https://typora-licodeao.oss-cn-guangzhou.aliyuncs.com/typoraImg/Rococo2.jpg'
            self.info.nickName = res.userInfo.nickName
            uni.setStorageSync("globalUser", res.userInfo)
            uni.login({
              provider: "weixin",
              success(loginResult) {
                self.info.code = loginResult.code
                // 缓存中有realName
                if (self.info.realName) {
                  uni.request({
                      url: self.serverUrl + "/wxLogin",
                      data: self.info,
                      method: "POST",
                      header: {
                        "Authorization": uni.getStorageSync('tokenHead') + ' ' + uni.getStorageSync('token')
                      }
                    }).then(res => {
                      console.log("缓存中有realName的wxlogin----------", res)
                      uni.setStorageSync('tokenHead', res[1].data.object.tokenHead)
                      uni.setStorageSync('token', res[1].data.object.token)
                      // uni.setStorageSync('authority', res[1].data.object.authority)
                      uni.setStorageSync('isLogin', true)
                      uni.reLaunch({
                        url: '../mine/mine'
                      })
                    })
                } else {
                  // 缓存中没有realName
                  self.info.isFirst = true
                  uni.request({
                      url: self.serverUrl + "/getRealName",
                      method: 'GET',
                      data: {
                        "code": self.info.code,
                      },
                      header: {
                        "Authorization": uni.getStorageSync('tokenHead') + ' ' + uni.getStorageSync('token')
                      }
                    }).then(res => {
                      console.log("缓存中没有realName-----------", res)
                      // 数据库中没有realName
                      if (res[1].data.code === 200 && res[1].data.message == null) {
                        self.show = true
                      } else if (res[1].data.code === 200 && res[1].data.message != null) {
                        //  数据库中有realName
                        self.info.isFirst = false
                        self.info.realName = res[1].data.message
                        uni.setStorageSync("realName", self.info.realName)
                        console.log(self.info.realName)
                        uni.request({
                            url: self.serverUrl + "/wxLogin",
                            data: self.info,
                            method: "POST",
                            header: {
                              "Authorization": uni.getStorageSync('tokenHead') + ' ' + uni.getStorageSync('token')
                            }
                          }).then(res => {
                            console.log("数据库中有realName------------", res)
                            uni.setStorageSync('tokenHead', res[1].data.object.tokenHead)
                            uni.setStorageSync('token', res[1].data.object.token)
                            // uni.setStorageSync('authority', res[1].data.object.authority)
                            uni.setStorageSync('isLogin', true)
                            uni.reLaunch({
                              url: '../mine/mine'
                            })
                          })
                      }
                    })
                }
              }
            })
          },
          fail: () => {
            uni.setStorageSync('isLogin', false)
          }
        })
      },
      changeAuthority() {
        uni.navigateTo({
          url: '../../subpkg/changeAuthority/changeAuthority'
        })
      },
      toUpdate() {
        uni.navigateTo({
          url: "../../subpkg/updateLog/updateLog"
        })
      },
      toGuide() {
        // uni.navigateTo({
        //   url: "../../subpkg/useGuide/useGuide"
        // })
        uni.showToast({
          icon: 'error',
          title: '还未编写',
          duration: 2000
        })
      },
      getRealName(e) {
        this.info.realName = e
      },
      getPhone(e) {
        this.info.phone = e
      },
      getPassword(e) {
        this.info.password = e
      },
      submitInfo() {
        let self = this
        if (self.info.realName !== " " && self.info.phone !== " " && self.info.password !== " ") {
          uni.request({
            url: self.serverUrl + '/wxLogin',
            data: self.info,
            method: 'POST',
            header: {
              "Authorization": uni.getStorageSync('tokenHead') + ' ' + uni.getStorageSync('token')
            }
          }).then(res => {
            console.log("submitInfo-----------------", res)
            uni.setStorageSync('tokenHead',res[1].data.object.tokenHead)
            uni.setStorageSync('token',res[1].data.object.token)
            // uni.setStorageSync('authority',res[1].data.object.authority)
            uni.setStorageSync('isLogin',true)
            uni.setStorageSync("realName",self.info.realName)
            uni.reLaunch({
              url: '../mine/mine'
            })
          })
        } else {
          uni.showToast({
            icon: 'error',
            title: '不能为空鸭',
            duration: 1500
          })
        }
      }
    }
  }
</script>

<style lang="scss">
  page {
    width: 100%;
    background: #ffffff;
  }

  .cu-list {
    border: 1px solid #eee;
  }

  .top {
    height: 600rpx;
    background-image: linear-gradient(to top, #cfd9df 0%, #e2ebf0 100%);
    position: relative;
  }

  .wave {
    height: 30px;
    position: absolute;
    width: 100%;
    z-index: 12;
    left: 0;
    bottom: 0;
    animation: wave-animation1 6.3s infinite linear;
    background: url('https://guli-shop123.oss-cn-guangzhou.aliyuncs.com/%E5%B0%8F%E7%A8%8B%E5%BA%8F/waving1.png') 0 0 repeat-x;
  }

  @keyframes wave-animation1 {
    0% {
      background-position: 0 0;
    }

    100% {
      background-position: 1601px 0;
    }
  }
</style>
